package bean.managed;

import bean.managed.util.JsfUtil;
import bean.managed.util.PaginationHelper;
import bean.session.CustomerEntityFacade;
import bean.session.SalesLeadEntityFacade;
import bean.session.SystemUserEntityFacade;


import bean.session.VendorEntityFacade;
import entity.CustomerEntity;
import entity.PersonEntity;
import entity.SalesLeadEntity;
import entity.SystemUserEntity;
import entity.VendorEntity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import managedbean.AbstractBacking;

@ManagedBean(name = "AccountUnlockController")
@SessionScoped
public class AccountUnlockController extends AbstractBacking implements Serializable {

    private PersonEntity selectedUser;

    public PersonEntity getSelectedUser() {
        return selectedUser;
    }

    public void setSelectedUser(PersonEntity selectedUser) {
        this.selectedUser = selectedUser;
        activateUser();
    }

    public SystemUserEntityFacade getFacade() {
        return Facade;
    }

    public void setFacade(SystemUserEntityFacade Facade) {
        this.Facade = Facade;
    }

    public SalesLeadEntityFacade getFacade1() {
        return Facade1;
    }

    public void setFacade1(SalesLeadEntityFacade Facade1) {
        this.Facade1 = Facade1;
    }

    public VendorEntityFacade getFacade2() {
        return Facade2;
    }

    public void setFacade2(VendorEntityFacade Facade2) {
        this.Facade2 = Facade2;
    }

    public CustomerEntityFacade getFacade3() {
        return Facade3;
    }

    public void setFacade3(CustomerEntityFacade Facade3) {
        this.Facade3 = Facade3;
    }
    @EJB
    private bean.session.SystemUserEntityFacade Facade;
    @EJB
    private bean.session.SalesLeadEntityFacade Facade1;
    @EJB
    private bean.session.VendorEntityFacade Facade2;
    @EJB
    private bean.session.CustomerEntityFacade Facade3;
    private Collection<PersonEntity> user;

    public AccountUnlockController() {
    }

    public String activateUser() {
        PersonEntity p = getSelectedUser();
        if (p instanceof SystemUserEntity) {
            p.setLocked(false);
            getFacade().edit((SystemUserEntity) p);
        } else if (p instanceof VendorEntity) {
            p.setLocked(false);

            getFacade2().edit((VendorEntity) p);
        } else if (p instanceof SalesLeadEntity) {
            p.setLocked(false);
            getFacade1().edit((SalesLeadEntity) p);
        } else if (p instanceof CustomerEntity) {
            p.setLocked(false);
            getFacade3().edit((CustomerEntity) p);
        }
        p.modified(getLoginPerson());
        user.remove(p);
        JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("UserUnlocked"));
        return "UnlockUser?faces-redirect=true";
    }
    private String status;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public void statusUpdate(String status) {
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage(status, null));
    }

    public Collection<PersonEntity> getUser() {
        user = new ArrayList<PersonEntity>();
        user.addAll(Facade.getLockedUser());
        user.addAll(Facade1.getLockedUser());
        user.addAll(Facade2.getLockedUser());
        user.addAll(Facade3.getLockedUser());
        return user;
    }
}
